#include "stdafx.h"
#include <mmsystem.h>
#include "Timer.h"

//-------------------------------------------------------------------------
// constructors
//-------------------------------------------------------------------------

CTimer::CTimer()
{
	LARGE_INTEGER frequency;
    m_bUsingQPF = QueryPerformanceFrequency(&frequency);
	m_llFrequency = frequency.QuadPart;
	m_fStartTime = 0.0;
	m_fStopTime = 0.0;
}

//-------------------------------------------------------------------------
// functions
//-------------------------------------------------------------------------

float CTimer::Timer(TIMER_COMMAND command)
{
    if (m_bUsingQPF)
    {      

		LARGE_INTEGER count;
		QueryPerformanceCounter(&count);

        // Start the timer

        if (command == START)
        {
			m_llStartCount = count.QuadPart;
            return 0.0f;
        }
    
        // Stop the timer

        if (command == STOP)
        {
			m_llEndCount = count.QuadPart;
            return 0.0f;
        }
    
        // Return the elapsed time

        if (command == GETELAPSEDTIME)
        {
			double fElapsedTime = (double) (m_llEndCount - m_llStartCount) / (double) m_llFrequency;
			return (FLOAT) fElapsedTime;
		}

        return -1.0f; // Invalid command specified
    }
    else
    {	
        double fTime;
		fTime = timeGetTime() * 0.001;

        // Start the timer

        if (command == START)
        {
			m_fStartTime = fTime;
            return 0.0f;
        }
    
        // Stop the timer

        if (command == STOP)
        {
			m_fStopTime = fTime;
            return 0.0f;
        }
 
        // Return the elapsed time

        if (command == GETELAPSEDTIME)
        {   
            return (FLOAT) (m_fStopTime - m_fStartTime);
        }

        return -1.0f; // Invalid command specified
    }
}